package mm1.GUI;
 
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.media.*;
import javafx.ext.swing.SwingButton;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.control.TextBox;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Flow;
import javafx.scene.Group;
import javafx.animation.Timeline;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.geometry.VPos;
import javafx.geometry.HPos;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;


/**
 * GUI-Mainclass: Build GUI (Layout)
 *
 * @author	Ralph Hoch, Matthias Heisler
 */
 
public class MediaPlayerGUI {
    var properties:Properties;
    
    init {
        properties = new Properties();
        println("LOADING PROPERTIES");
       	
        properties.load(new FileInputStream("config/video.properties"));
        //println({properties.get("VOLUME")});
        //println({java.lang.Integer.parseInt(properties.get("VOLUME"))});
    }
    
    postinit {
		var img:Image = Image {
		    url: "{new File("img/shake_1.png").toURI()}"
		    //url: "{appPath}img/shake_1.png"
		}
		var shake_1 = Image {
		    url: "{new File("img/shake_1.png").toURI()}"
		    //url: "{appPath}img/shake_1.png"
			width: 50
			preserveRatio: true
		}
		var center_1 = Image {
		    url: "{new File("img/center_1.png").toURI()}"
			//url: "{appPath}img/center_1.png"
			width: 50
			preserveRatio: true
		}
		
		var alphaSlider = Slider {
			vertical: true
			//value: 75
			value: java.lang.Integer.parseInt(properties.get("ALPHA"))
		   	max: 100
		}
		
		var centerPos:Boolean = false;
	    
	    /**
	     * The videoscreen.
	     * This variable contains the layout of and the media player
	     *
	     */
	    var vds:VideoScreen = VideoScreen {
	        movieWidth: bind scene.width * 0.75;
	        movieHeight: bind scene.height * 0.75;
	        volume: bind mpControls.volumeSlider.value / 100;
	        centerPos: bind centerPos;
	    }
	    
	    /**
	     * The controlsscreen.
	     * This variable contains the layout of the controls use for the video player
	     *
	     */
	    var mpControls:MediaPlayerControlsScreen = MediaPlayerControlsScreen {
	        volumeSlider: Slider {
	        	//value: 75
	        	value: java.lang.Integer.parseInt(properties.get("VOLUME"))
	        	max : 100
	        } 
	    	vds: vds
	    }
	    
	    /**
	     * The openscreen.
	     * This variable contains the layout of the controls used to open video files
	     *
	     */
	    var openControls:OpenControlsScreen = OpenControlsScreen {
	    	isVisible: bind mpControls.openScreenVisible;
	    	vds: vds;
	    }
	    
	    var brightSlider = Slider {
	    	vertical: true
	    	value: 75
	   		max: 100
	    }
	    
	    var startBox:TextBox = TextBox {
	    	text: properties.get("START")
	    } 
	    

	    var durationBox:TextBox = TextBox {
	    	text: properties.get("DURATION")
	    } 
	    
	    /**
	     * The actual layout
	     *
	     */
		def scene: Scene = Scene {
			fill: Color.rgb(201,184,195,1.0)
			content: 
				HBox {
					content: [
						VBox { 
							width: bind scene.width * 0.8
							content: [
								VBox {
									translateY: 10
									height: bind scene.height * 0.8
									content: [
										vds
									]
								}
								VBox {
									height: bind scene.height * 0.2
									vpos: VPos.BOTTOM
									content: [
										mpControls,
										openControls
									]
								}
							]
						},
						VBox {
							width: bind scene.width * 0.2
							hpos: HPos.RIGHT
							spacing: 10
							content: [
								VBox {
									translateX: -2
									content: [
										Label {text: "Start:"},
										startBox,
										Label {text: "Dauer"},
										durationBox
									]
								}
								VBox {
									translateX: 18
									content: [
										ImageView {
				   					   	image: center_1
					   					   	onMouseClicked: function(e: MouseEvent): Void {
					   					   		println("SET CENTER");
					   					   		centerPos = not centerPos;
					   					   	}
				   					   	}
										ImageView {
				   					   	image: shake_1
					   					   	onMouseClicked: function(e: MouseEvent): Void {
					   					   		println("SHAKE IT");
					   					   	}
				   					   	}
									]
								}
								VBox {
									spacing: 4
									content: [
										HBox {
											translateX: 12
											content: [
												Label {text: "Brightness"}
											]
										}
										HBox {
											translateX: 30
											/**
											* The slider needs to be rotated by 180, otherwise
											* the maximum is at the bottom. 
											*
											*/
											rotate: 180 
											content: [
								        		alphaSlider
											]
										}
									]
								}
							]
						}
					]
				} 
		}//Scene
	    
	    def stage:Stage = Stage {
	        title: "MediaPlayer MM1"
	        width: bind stageWidth with inverse
	        height: bind stageHeight with inverse
	        onClose: function() {
	       		properties.put("VOLUME", "{java.lang.Math.round(mpControls.volumeSlider.value)}");
	       		properties.put("ALPHA", "{java.lang.Math.round(alphaSlider.value)}");
	       		properties.put("POS_X", "50");
	       		properties.put("POS_Y", "50");
	       		properties.put("START", startBox.text);
	       		properties.put("DURATION", durationBox.text);
	        	properties.store(new FileOutputStream("config/video.properties"));
	
	            println("WRITING CONFIG-FILE");
	        }
	        scene: scene
	    }
	    
	    /**
	     * Contains the width of the application
	     *
	     */
	    var stageWidth = stage.width on replace {
	    	if (stageWidth < 640) stageWidth = 640;	
	    }
	    
	    /**
	     * Contains the height of the application
	     *
	     */
	    var stageHeight = stage.height on replace {
	    	if (stageHeight < 480) stageHeight = 480;	
	    }
	    
    }
    
    

    

}

